﻿using BLL;
using DBUtility;
using EncryptHelper;
using gfLibrary;
using IYAKASAPortal.Services;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IYAKASAPortal.Authority
{
    public partial class ChangePW : System.Web.UI.Page
    {
        private UserInfo userInfo { get; set; }

        private static string userName { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] != null && Session["UserName"].ToString() != "")
            {
                userName = Session["UserName"].ToString();
                userInfo = IYAKASAPortal.Services.UserAuthority.GetUserAuthority(userName);
            }
     
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string stroldpw = txtOPwd.Text.Trim();
            if (string.IsNullOrEmpty(stroldpw))
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "请输入旧密码!");
                txtOPwd.Focus();
                return;
            }

            txtOPwd.Attributes["value"] = "";
            string oldPassword = "";
            oldPassword = Encrypt.MD5Encrypt(userName + txtOPwd.Text.Trim());
            EMPLOYEE_USER_BLL o_User_BLL = new EMPLOYEE_USER_BLL();
            EMPLOYEE_USER o_User = o_User_BLL.GetModelList("EMPLOYEE_ID='" + userName + "'")[0];

            if (o_User.EMPLOYEE_PWD != oldPassword)
            {
                gfLibrary.gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "旧密码密码错误!!");
                txtOPwd.Focus();
                return;
            }

            DbFactory m_df = new DbFactory();
            EMPLOYEE_USER_BLL m_User_BLL = new EMPLOYEE_USER_BLL();
            EMPLOYEE_USER m_User = new EMPLOYEE_USER();

            int intID = o_User.ID;

            m_User.EMPLOYEE_PWD=Encrypt.MD5Encrypt(userName + txtEPwd.Text.Trim()); ;
            
            m_df.Add(m_User_BLL.UpdateSQL(m_User,intID));

            if (m_df.Execute() == true)
            {
                gfLibrary.gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "修改密码成功!");
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "xxxe", "window.location.href='../frame_main.aspx';", true);
            }
            else
            {
                gfLibrary.gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "修改密码失败!");
                txtOPwd.Attributes["value"] = "";
            }
        }
    }
}